<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SubMessageController extends Controller
{
    /**
     * 是否提示显示订阅消息弹窗，并且返回模板消息id
     */
    public function testShowSubMessage(Request $request)
    {
        try {
            // type订阅类型 media=素材库审核通过 coupon=卡券审核通过
            $_type = $request->type;
            // 1、用户是否关注公众号
            $Wchat = $this->wechat();
            $Fans = $Wchat->user->get(auth("member")->user()->openid);
            if($Fans["subscribe"]) {
                throw new \Exception("已关注公众号", 402);
            }
            // 2、系统是否开启了对应功能的审核
            $Setting = $this->getConfig();
            if($_type == 'media') {
                if(!$Setting->media->is_check) {
                    throw new \Exception("未开启素材审核",402);
                }
            } else if($_type == 'coupon') {
                if(!$Setting->coupon_check) {
                    throw new \Exception("未开启卡券审核",402);
                }
            } else {
                throw new \Exception("未知类型",402);
            }
            if(empty($Setting->message->result_check_sub)) {
                throw new \Exception("未设置模板id", 402);
            }
            return response(["data"=>$Setting->message->result_check_sub]);
        }catch (\Exception $e) {
            return response(['message'=>$e->getMessage()],$e->getCode());
        }
    }
}
